package com.sailfish.springbootdemo.controller.db2;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.BoardInfo;
import com.sailfish.springbootdemo.pojo.db2.PcbConfig;
import com.sailfish.springbootdemo.pojo.db2.PcbCost;
import com.sailfish.springbootdemo.pojo.db2.PcbCostByFactory;
import com.sailfish.springbootdemo.service.db2.PcbCostService;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.HttpUtil;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/pcbCost")
public class PcbCostController {
//    @Value("${personal.config.apis.sign}")
//    private String sign;
//    @Value("${third_url_prefix.axi-url}")
//    private String getAxiApi;

    @Autowired
    PcbCostService pcbCostService;

    @PostMapping("/getAllCostList")
    public Result getAllCostList(String productName, String productVersion, Integer factoryId) {
        PcbCost search = new PcbCost();
        search.setBoardName(productVersion);
        search.setVersion(productName);
        search.setFactoryId(factoryId);
        List<PcbCost> list = pcbCostService.getAll(search);
        return ResultUtil.success(list);
    }

    @PostMapping("/getByFactory")
    public Result getByFactory() {
        List<PcbCost> list = pcbCostService.getByFactory();
        return ResultUtil.success(list);
    }

    @PostMapping("/getCostBoardList")
    public Result getCostBoardList(String searchK,String searchV,String timeRange1,String timeRange2,Integer page, Integer pageSize) {
        Result res = pcbCostService.getCostBoardList(searchK,searchV,timeRange1,timeRange2,page,pageSize);
        return res;
    }



    @PostMapping("/insertPcbCost")
    public Result insertPcbCost(@RequestBody String data) {
        return pcbCostService.insert(data);
    }

    @PostMapping("/updatePcbCost")
    public Result updatePcbCost(@RequestBody String data) {
        return pcbCostService.update(data);
    }

    @PostMapping("/deletePcbCost")
    public Result deletePcbCost(Integer id) {
        return pcbCostService.delete(id);
    }

    @PostMapping("/deletePcbCostByProduct")
    public Result deletePcbCostByProduct(String productName, String productVersion) {
        return pcbCostService.deletePcbCostByProduct(productName, productVersion);
    }
    @PostMapping("/output")
    public Result output() {
        Result result = pcbCostService.output();
        if (result.getCode().equals(200)) {
            try {
                String filepath = (String) result.getData();
                File file = new File(filepath);
                String fname = file.getName();
                byte[] bytedata = null;
                InputStream in = new FileInputStream(file);  //InputStream in = url.openStream();
                bytedata = new byte[in.available()];
                in.read(bytedata);
                in.close();
                Base64.Encoder encoder = Base64.getEncoder();
                String base64 = encoder.encodeToString(bytedata);

                Map<String, Object> map = new HashMap<>();
                map.put("base64", base64);
                map.put("fileName", fname);
                return ResultUtil.success(map);

            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
            }
        } else {
            return result;
        }
    }
}
